package com.epam.iunna.bookshop.controller.actions;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.iunna.bookshop.controller.application.Constants;
import com.epam.iunna.bookshop.controller.utils.dispatch.ActionInt;
import com.epam.iunna.bookshop.model.ItemDao;

public class RemoveItem implements ActionInt{

    @Override
    public String perform(HttpServletRequest request,
            HttpServletResponse response) {

        Long itemID = Long.valueOf(0);
        try{
            itemID = Long.parseLong(request.getParameter("itemID"));
            ItemDao itemDao = new ItemDao();
            itemDao.removeItem(itemID);
        }catch(NumberFormatException e){
            System.out.println("Incorrect item id " + itemID );
            e.printStackTrace();
            return Constants.FATAL_ERROR_PAGE_PATH;
        }catch(SQLException e){
            System.out.println("Db error ");
            e.printStackTrace();
            return Constants.FATAL_ERROR_PAGE_PATH;
        }
        BooksList bookListAction = new BooksList();
        return bookListAction.perform(request, response);
    }

}
